package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.dto.maintain.MaintainDTO;
import com.whfc.mach.param.maintain.MachMaintainAddParam;
import com.whfc.mach.param.maintain.MachMaintainEditParam;
import com.whfc.ms.api.mach.service.MsMachMaintainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * 设备保养模块
 *
 * @author sun_guodong
 * @date 2021-08-09
 */
@RestController
@RequestMapping("/ms/api/v1/mach/maintain")
public class MsMachMaintainController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachMaintainService msMachMaintainService;

    @PostMapping(value = {"/anon/upload", "/upload"})
    public Result upload(@RequestParam("file") MultipartFile file) {
        logger.info("上传保养文件服务,fileName:{}", file.getOriginalFilename());
        OssPathDTO pathDTO = msMachMaintainService.upload(file);
        return ResultUtil.success(pathDTO);
    }

    @GetMapping("/file/anon/getConfig")
    public Result getConfig(@RequestParam("name") String name) {
        logger.info("获取文件上传配置参数,name:{}", name);
        Map<String, String> map = msMachMaintainService.getConfig(name);
        return ResultUtil.success(map);
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "machType", required = false) Integer machType) {
        logger.info("设备保养记录列表,loginer:{},deptId:{},pageNum:{},pageSize:{},startDate:{},endDate:{},keyword:{},machType:{}",
                user.getNickname(), deptId, pageNum, pageSize, startDate, endDate, keyword, machType);
        PageData<MaintainDTO> pageData = msMachMaintainService.list(deptId, pageNum, pageSize, startDate, endDate, keyword, machType);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/listByMachId")
    public Result listByMachId(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("machId") Integer machId,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        logger.info("设备保养记录列表(单台设备),loginer:{},machId:{},pageNum:{},pageSize:{}",
                user.getNickname(), machId, pageNum, pageSize);
        PageData<MaintainDTO> pageData = msMachMaintainService.listByMachId(machId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("maintainId") Integer maintainId) {
        logger.info("设备保养详情,loginer:{},maintainId:{}", user.getNickname(), maintainId);
        MaintainDTO data = msMachMaintainService.detail(maintainId);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("mach/maintain/record/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated MachMaintainAddParam param) {
        logger.info("添加保养记录,loginer:{},param:{}", user.getNickname(), param.toString());
        param.setMaintainUserId(user.getId());
        param.setMaintainUserName(user.getNickname());
        msMachMaintainService.add(param);
        return ResultUtil.success();
    }

    @PostMapping(value = {"/anon/addByH5", "/addByH5"})
    public Result addByH5(@RequestBody @Validated MachMaintainAddParam param) {
        logger.info("移动端添加保养记录,param:{}", param.toString());
        param.setMaintainUserId(0);
        param.setMaintainUserName("移动端扫码提交");
        msMachMaintainService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/maintain/record/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated MachMaintainEditParam param) {
        logger.info("编辑保养记录,param:{}", param.toString());
        msMachMaintainService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/maintain/record/del")
    @PostMapping("/del/{maintainId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("maintainId") Integer maintainId) {
        logger.info("删除保养记录,maintainId:{}", maintainId);
        msMachMaintainService.del(maintainId);
        return ResultUtil.success();
    }

    @PostMapping("/export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate,
                         @RequestParam(value = "keyword", required = false) String keyword,
                         @RequestParam(value = "machType", required = false) Integer machType) {
        logger.info("导出保养记录,loginer:{},deptId:{},startDate:{},endDate:{},keyword:{},machType:{}",
                user.getNickname(), deptId, startDate, endDate, keyword, machType);
        msMachMaintainService.export(deptId, startDate, endDate, keyword, machType);
        return ResultUtil.success();
    }

    @GetMapping("/getAvailableMach")
    public Result getAvailableMach(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("deptId") Integer deptId,
                                   @RequestParam("machType")  Integer machType) {
        logger.info("获取可供选择保养的设备,loginer:{},deptId:{},machType:{}", user.getNickname(), deptId, machType);
        ListData<AppMachDTO> listData = msMachMaintainService.getAvailableMach(deptId, machType);
        return ResultUtil.success(listData);
    }

}
